package extractLatAndLon;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class latAndLonSourceFromYago {

	public static void main(String[] args) {
		String inFilePath = "F:/yago/yagoGeonamesData.ttl";
		String latPath = "geoLatInput.ttl";
		String lonPath = "geoLonInput.ttl";
		File inFile = new File(inFilePath);
		File latOutputFile = new File(latPath);
		File lonOutputFile = new File(lonPath);
		try {
			FileReader inReader = new FileReader(inFile.getAbsolutePath());
			FileWriter latWriter = new FileWriter(latOutputFile.getAbsolutePath(), true);
			FileWriter lonWriter = new FileWriter(lonOutputFile.getAbsolutePath(), true);
			BufferedReader bReader = new BufferedReader(inReader);
			BufferedWriter bLatWriter = new BufferedWriter(latWriter);
			BufferedWriter bLonWriter = new BufferedWriter(lonWriter);
			String aLine = "", lat = "", lon = "";
			while ((aLine = bReader.readLine()) != null) {
				if (aLine.contains("<hasLatitude>")) {
					Pattern pattern = Pattern.compile("-{0,1}[0-9]+(\\.[0-9]+){0,1}");
					Matcher matcher = pattern.matcher(aLine);
					if (matcher.find()) {
						lat = matcher.group(0);
						bLatWriter.write(lat + "\n");
						bLatWriter.flush();
					}
				}
				if (aLine.contains("<hasLongitude>")) {
					Pattern pattern = Pattern.compile("-{0,1}[0-9]+(\\.[0-9]+){0,1}");
					Matcher matcher = pattern.matcher(aLine);
					if (matcher.find()) {
						lon = matcher.group(0);
						bLonWriter.write(lon + "\n");
						bLonWriter.flush();
					}
				}
			}
			inReader.close();
			bReader.close();
			bLatWriter.close();
			bLonWriter.close();
			latWriter.close();
			lonWriter.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
